# -*- coding: utf-8 -*-
import json,os
from jinja2 import Markup
from flask import flash, url_for, redirect, request, current_app
from flask_admin.base import expose
from flask_admin.model.template import EndpointLinkRowAction, LinkRowAction
from flask_wtf import FlaskForm
from sqlalchemy import func
from wtforms import SelectMultipleField, ValidationError
from wtforms.validators import DataRequired
from flask_admin.actions import action
from flask_admin.helpers import (get_form_data, validate_form_on_submit,get_redirect_target, flash_errors)
from flask_admin.contrib.sqla import form, filters as sqla_filters, tools
from adminApp.views import MyModelView
from adminApp.views import AjaxQueryIndexLike
from adminApp.extensions import db, redis_store


class WarnLogsView(MyModelView):
    can_create = False
    can_edit = False
    column_filters = ('machine.idc.name', 'machine.hostname', 'warn_type', 'title', 'status', 'remark', 'created')

    column_list = ('id', 'machine.idc.name', 'machine.hostname', 'warn_type', 'title', 'status', 'remark', 'created')

    column_editable_list = ('remark', 'status')

    column_choices ={
        'status': [('wait', '待处理'), ('work', '处理中'), ('over', '处理完毕')]
    }

    edit_modal = True
    details_modal = True

    column_labels = {
        'machine.idc.name': '机房',
        'machine.hostname': '主机名称',
        'warn_type': '警报类型',
        'title': '标题',
        'status': '状态',
        'remark': '备注',
        'created': '创建日期',
        'updated': '更新日期',
    }

    column_formatters = dict(
        created=lambda v, c, m, p: m.created.strftime("%Y-%m-%d %H:%M:%S"),
        updated=lambda v, c, m, p: m.updated.strftime("%Y-%m-%d %H:%M:%S") if m.updated else ''
    )